<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 角色管理模块 -->
          <el-tab-pane label="角色管理">
            <el-button
              size="small"
              type="primary"
              icon="el-icon-plus"
              style="margin-bottom: 20px"
              @click="isShowDia = true"
            >新增角色</el-button>
            <!-- 表单 -->
            <el-table border :data="list">
              <el-table-column
                type="index"
                label="序号"
                width="120"
                align="center"
              />
              <el-table-column
                prop="name"
                label="名称"
                width="240"
                align="center"
              />
              <el-table-column prop="description" label="描述" align="center" />
              <el-table-column label="操作" align="center">
                <template slot-scope="{ row }">
                  <el-button
                    type="success"
                    @click="assignBtn(row.id)"
                  >分配权限</el-button>
                  <el-button
                    type="primary"
                    @click="editbtn(row.id)"
                  >编辑</el-button>
                  <el-button
                    type="danger"
                    @click="deleteRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- // 分页按钮 -->
            <el-row
              type="flex"
              justify="center"
              style="height: 60px"
              align="middle"
            >
              <el-pagination
                layout="prev, pager, next"
                :current-page="page.page"
                :total="page.total"
                :page-size="page.pagesize"
                @current-change="changePage"
              />
            </el-row>
          </el-tab-pane>
          <!-- // 公司信息模块 -->
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form
              label-width="80px"
              style="margin-top: 25px"
              label-position="left"
            >
              <el-form-item label="公司名称">
                <el-input
                  v-model="formData.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="formData.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="formData.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="formData.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- // 放置一个弹层组件 -->
    <el-dialog
      width="40%"
      :visible="isShowDia"
      title="编辑角色"
      @close="btnCancel"
    >
      <el-form
        ref="ruleForm"
        label-width="120px"
        :model="ruleForm"
        :rules="rules"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="ruleForm.name" style="width: 400px" />
        </el-form-item>
        <el-form-item label="角色描述">
          <el-input v-model="ruleForm.description" style="width: 400px" />
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="5.8">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOk">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <!-- 分配权限弹层 -->
    <el-dialog title="分配权限" :visible.sync="dialogVisible">
      <el-tree
        ref="tree"
        :data="treeList"
        :default-expand-all="true"
        node-key="id"
        :props="defaultProps"
        :show-checkbox="true"
        :check-strictly="true"
        :default-checked-keys="deffaltList"
      />
      <el-row slot="footer" type="flex" justify="center" class="dialog-footer">
        <el-col :span="6">
          <el-button
            size="small"
            @click="dialogVisible = false"
          >取 消</el-button>
          <el-button
            type="primary"
            size="small"
            @click="btnPermOK"
          >确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permisson'
import { tranListToTreeData } from '@/utils'
import {
  getRoleList,
  getCompanyInfo,
  deleteRole,
  updateRole,
  getRoleDetail,
  addRole,
  roleCompetence
} from '@/api/setting'
import store from '@/store/index'
export default {
  data() {
    return {
      deffaltList: [], // 默认选中的权限点
      defaultProps: {
        label: 'name'
      },
      treeList: [], // 所有树形权限点
      dialogVisible: false, // 分配权限弹层开关
      list: [], // 角色列表
      page: {
        page: 1, // 当前页码
        pagesize: 4, // 每页的数量
        total: 0 // 记录总数
      },
      formData: {}, // 所有角色列表
      isShowDia: false,
      // 专门接收新增或者编辑的编辑的表单数据
      ruleForm: {},
      roleId: null,
      rules: {
        name: [{ required: true, message: '角色名称不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 点击分配权限弹层的确定按钮
    async btnPermOK() {
      await roleCompetence({
        id: this.roleId,
        permIds: this.$refs.tree.getCheckedKeys()
      })
      this.$message.success('分配权限点成功')
      this.dialogVisible = false
    },
    // 点击分配权限按钮
    async assignBtn(id) {
      this.roleId = id
      const { permIds } = await getRoleDetail(id)
      this.deffaltList = permIds
      this.treeList = tranListToTreeData(await getPermissionList(), '0')
      this.dialogVisible = true
    },
    // 点击确定按钮
    async btnOk() {
      try {
        await this.$refs.ruleForm.validate()
        if (this.ruleForm.id) {
          // 有id为编辑接口
          await updateRole(this.ruleForm)
          this.$message.success('修改成功')
        } else {
          await addRole(this.ruleForm)
          this.$message.success('添加成功')
        }
        this.getRoleList()
        this.isShowDia = false
      } catch (error) {
        console.log(error)
      }
    },
    // 点击编辑里的取消按钮
    btnCancel() {
      this.isShowDia = false
      this.$refs.ruleForm.resetFields()
      this.ruleForm = {}
    },
    // 点击编辑按钮
    async editbtn(id) {
      this.ruleForm = await getRoleDetail(id)
      this.isShowDia = true
    },
    // 点击页码时获取当前页数据
    changePage(newpage) {
      this.page.page = newpage
      this.getRoleList()
    },
    // 获取所有角色列表
    async getRoleList() {
      const { total, rows } = await getRoleList(this.page)
      this.list = rows
      this.page.total = total
    },
    // 获取公司信息列表
    async getCompanyInfo() {
      this.formData = await getCompanyInfo(store.getters.companyId)
    },
    // 删除角色
    async deleteRole(id) {
      try {
        await this.$confirm('确认删除该角色吗', '提示', {
          type: 'warning'
        })
        await deleteRole(id)
        await this.getRoleList()
        this.$message.success('删除角色成功')
      } catch (error) {
        console.log(error)
      }
    }
  }
}
</script>

<style></style>
